package com.itheima.leetcode.od.c.sort;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

/**
 * <h3>开源项目热度榜单</h3>
 */
public class OpenProjectRank {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int count = in.nextInt();
        int[] priority = new int[5];
        for (int i = 0; i < 5; ++i) {
            priority[i] = in.nextInt();
        }

        List<Project> all_project = new ArrayList<>();
        // family relationship
        for (int i = 0; i < count; i++) {
            String name = in.next();

            int j = 0;
            int total_score = 0;
            while (j < 5) {
                int temp_score = in.nextInt();
                total_score += temp_score * priority[j];
                j += 1;
            }

            Project project = new Project(name, total_score);
            all_project.add(project);
        }

        Collections.sort(all_project);

        System.out.println(all_project.stream()
                .map(project -> project.name)
                .collect(Collectors.joining("\n")));
    }

    public static class Project implements Comparable<Project> {
        String name;
        int score;

        public Project(String name, int score) {
            this.name = name;
            this.score = score;
        }

        //自定义排序
        @Override
        public int compareTo(Project o) {
            if (o.score != this.score) {
                return o.score - this.score;
            } else {
                return this.name.toLowerCase().compareTo(o.name.toLowerCase());
            }
        }
    }
}